Audio Player
The AVPlayer class provides audio and video playback capabilities with support for playback control, looping, event callbacks, and metadata retrieval.
You can use setSource() to set a media source (either a local file or a remote URL) and call play() to start playback.
Getting Started
Example usage of AVPlayer:
API Reference
Properties
volume: number
Controls the playback volume, ranging from 0.0 (muted) to 1.0 (maximum).
duration: DurationInSeconds
The total duration of the media in seconds.
This value will be 0 until the media is fully loaded.
currentTime: DurationInSeconds
The current playback time in seconds. You can set this value to seek to a specific time.
rate: number
Controls the playback speed.
A value of 1.0 is normal speed; values below 1.0 slow down playback, and values above 1.0 speed it up.
timeControlStatus: TimeControlStatus
Indicates the current playback state. Possible values:
paused: playback is pausedwaitingToPlayAtSpecifiedRate: waiting for conditions to start (e.g., buffering)playing: currently playing
numberOfLoops: number
Sets how many times the media will loop.
0: no looping- positive value: specific number of loops
- negative value: infinite looping
Methods
setSource(filePathOrURL: string): boolean
Sets the media source for playback. Accepts a local file path or a remote URL.
Returns:
trueif successfully setfalseif setting failed
play(): boolean
Starts playback of the current media.
Returns:
trueif playback started successfullyfalseif it failed to start
pause()
Pauses the current playback.
stop()
Stops playback and resets the position to the beginning.
dispose()
Releases all player resources and removes observers. Call this method when the player is no longer needed to avoid memory leaks.
loadMetadata(): Promise<AVMetadataItem[] | null>
Loads detailed metadata for the current media file.
Returns:
- An array of
AVMetadataItemobjects nullif no metadata is available or no source has been set
Example:
loadCommonMetadata(): Promise<AVMetadataItem[] | null>
Loads the common metadata of the current media, where each AVMetadataItem provides a commonKey that can be used across media formats.
Example:
Callbacks
onReadyToPlay?: () => void
Called when the media is ready for playback.
onTimeControlStatusChanged?: (status: TimeControlStatus) => void
Called when the playback state changes, such as from “waiting” to “playing.”
onEnded?: () => void
Called when playback finishes.
onError?: (message: string) => void
Called when an error occurs during playback. Receives an error message as an argument.
Audio Session Handling
AVPlayer relies on the system’s shared audio session.
You can configure it using SharedAudioSession to ensure correct playback behavior.
Handling interruptions (such as phone calls):
Common Use Cases
Play Remote Audio
Play Local File
Loop Playback
Retrieve Metadata
Best Practices
-
Resource Management Always call
dispose()after playback to release system resources. -
Error Handling Implement the
onErrorcallback to handle playback issues gracefully (e.g., network failures). -
Interruption Management Use audio session interruption listeners to pause and resume playback smoothly.
-
UI State Updates Use
onTimeControlStatusChangedto update your UI when the playback state changes. -
Metadata Usage Use
loadCommonMetadata()to retrieve general information such as title, artist, or album artwork for display in your app’s UI.
